package com.zimi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zimi.model.*;
import com.zimi.service.WfActivityAuthorityService;
import com.zimi.service.WfActivityService;
import com.zimi.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("流程管理")
@RestController
@RequestMapping("wf/activity/authority")
public class WfActivityAuthorityApi {

    @Autowired
    private WfActivityAuthorityService service;
    @Autowired
    private WfActivityService wfActivityService;

    @PostMapping("userTree")
    @ResponseBody
    @ApiOperation("查询所有")
    public Object userTree() {
        Map map = service.userTree();
        map.put("code", 0);
        return map;
    }

    @PostMapping("list")
    @ApiOperation("查询所有")
    public Object list(WfActivityAuthority record) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            List<SysOrgExtend> resList = new ArrayList<>();
            List<WfActivityAuthority> authList = service.selectAll(record);

            for (WfActivityAuthority auth : authList) {
                SysOrgExtend newAuth = new SysOrgExtend();
                Integer flag = Integer.parseInt(auth.getActivityType());
                newAuth.setFlag(flag); //auth类型135 467
                newAuth.setFunctionId(auth.getRole()); //userid，orgid …… 对应 auth类型

                switch (flag) {
                    case 1:
                        SysOrg org = new SysOrg().selectById(auth.getRole());
                        newAuth.setName(org.getOrgName()); //userName，orgName …… 对应 auth类型
                        newAuth.setOrgName("机构"); //auth 类型 名称 …… 对应 auth类型
                        break;
                    case 3:
                        String postName = "";
                        SysPost post = new SysPost();
                        SysOrg iorg = new SysOrg();
                        if ((auth.getRole() == 0 || auth.getRole() == null) && (auth.getOrgId() > 0)) {
                            iorg = iorg.selectById(auth.getOrgId());
                            SysOrg iogn = new SysOrg().selectById(iorg.getOgnId());
                            postName = String.format("%s-%s", iorg.getOrgName(), iogn.getPostName());
                        } else {
                            post = post.selectById(auth.getRole());
                            iorg = iorg.selectById(post.getOrgId());
                            SysDuty iduty = new SysDuty().selectById(post.getDutyId());
                            postName = String.format("%s-%s", iorg.getOrgName(), iduty.getDutyName());
                        }

                        newAuth.setOrgId(iorg.getId());
                        newAuth.setName(postName);
                        newAuth.setOrgName("岗位");

                        break;
                    case 4:
                        SysRole role = new SysRole().selectById(auth.getRole());
                        newAuth.setName(role.getRoleName());
                        newAuth.setOrgName("角色");
                        break;
                    case 5:
                        SysUser user = new SysUser().selectById(auth.getRole());
                        newAuth.setName(user.getUserName());
                        newAuth.setOrgName("用户");
                        break;
                    case 6:
                        SysCode sysDict = new SysCode();
                        sysDict.setCodeIsParent(2);
                        sysDict.setCodeTypeName("liuchengcanyuzheteshuleibie");
                        sysDict.setCodeValue(auth.getRole());

                        SysCode tempDict = sysDict.selectOne(new QueryWrapper<SysCode>().lambda().setEntity(sysDict));
                        newAuth.setName(tempDict.getCodeName());
                        newAuth.setOrgName("计算");
                        break;
                    case 7:
                        WfActivity wfActivity = wfActivityService.selectByPrimaryKey(auth.getRole());
                        newAuth.setName(wfActivity.getActivityName());
                        newAuth.setOrgName("指定环节办理人");
                        break;
                }

                resList.add(newAuth);
            }


            objectMap.put("List", resList);
        } catch (Exception e) {
            objectMap.put("OK", "error");
            objectMap.put("code", 400);
            objectMap.put("message", e.getMessage());
            return objectMap;
        }
        objectMap.put("OK", "OK");
        objectMap.put("code", 200);
        return objectMap;
    }

    @GetMapping("{id}")
    @ApiOperation("根据Id查询信息")
    public WfActivityAuthority selectByPrimaryKey(@PathVariable("id") int id) {
        return service.selectByPrimaryKey(id);
    }

    @DeleteMapping("{id}")
    @ApiOperation("根据Id删除")
    public boolean deleteByPrimaryKey(@PathVariable("id") int id) {
        service.deleteByPrimaryKey(id);
        return true;
    }

    @PostMapping("record")
    @ApiOperation("添加")
    public boolean insert(WfActivityAuthority record) {
        return service.insertSelective(record) > 0 ? true : false;
    }

    @PutMapping("record")
    @ApiOperation("根据条件修改信息")
    public boolean updateByPrimaryKey(WfActivityAuthority record) {
        service.updateByPrimaryKeySelective(record);
        return true;
    }

    @PostMapping("getAllByUser")
    @ApiOperation("根据当前登录用户获取")
    public Map getAllByUser() {
        Map result = new HashMap();
        result.put("List", service.getAllByUser());
        result.put("OK", "OK");
        result.put("code", 200);
        return result;
    }

    @PostMapping("getUserAll")
    @ApiOperation("得到所有该用户所有的流程权限数据记录")
    public Object getUserAll() {
        Map<String, Object> map = new HashMap<String, Object>();
        SysUser sysUser = UserUtil.getUser();
        map.put("List", service.getUserAll(sysUser));
        map.put("OK", "OK");
        map.put("code", 200);
        return map;
    }
}
